import GSTC from "gantt-schedule-timeline-calendar/dist/gstc.wasm.esm.min.js";
import axios from "axios";
const GSTCID = GSTC.api.GSTCID;
const date = GSTC.api.date;
const setEndTime = (start) => {
  return GSTC.api.date(start).add(12, "hour").endOf("hour");
};
function labelFunc({ item, vido }) {
  return vido.html`<div class="my-item"}>My item as lit-html</div>`;
}
function generate(data) {
  const rows = {};
  const items = {};
  let data1 = data;
  data1.map((item, index) => {
    const id = GSTC.api.GSTCID((index + 1).toString());
    rows[id] = item.row;
    items[id] = item.item;
  });
  return { rows, items };
}
const getLins = async () => {
  const res = await axios.get("/api/demolink/link");
  return res.data.data.link;
};
const getLevelData6 = async () => {
  const line = await getLins();
  console.log(line);
  const { data } = await axios.get(`/api/dataimport/listparent`);
  const rows = {};
  const items = {};
  let data1 = data.data.dataList;
  data1.map((item, index) => {
    const id = GSTCID(item.id);
    let row = {};
    row.id = GSTCID(item.id);
    row.task = item.name;
    row.startTime = date(item.datastart).valueOf();
    row.endTime = date(item.dataend).valueOf();
    row.taskTime = item.allday?.toString();
    row.parentId =
      item.parent && item.parent != item.id ? GSTCID(item.parent) : null;
    row.level = item.code.length / 2;
    row.code = item.code;
    row.expanded = false;
    row.prepath = item.prepath;
    rows[id] = row;
    let item_1 = {};
    item_1.id = GSTCID(item.id);
    item_1.rowId = GSTCID(item.id);
    item_1.label = item.id;
    item_1.time = {};
    item_1.time.start = date(item.datastart).add(8, "hour").valueOf();
    item_1.time.end = setEndTime(
      date(item.datastart).add(8, "hour").valueOf()
    ).valueOf();
    item_1.style =
      row.level != 6
        ? { display: "none", backgroundColor: "#7E349D" }
        : { backgroundColor: "#7E349D" };
    item_1.dependant = [];
    item_1.labelFunc = labelFunc;
    item_1.lineContent = `${row.task}-(${row.taskTime})`;
    items[id] = item_1;
  });
  for (let i in items) {
    line.map((li, index) => {
      if (li.source == i.split("-")[1] && rows[i].level === 6) {
        items[i].dependant.push(GSTCID(li.target));
      }
    });
  }
  return { rows, items };
};
const getLevelData5 = async () => {
  const line = await getLins();
  console.log(line);
  const { data } = await axios.get(`/api/dataimport/listparent`);
  const rows = {};
  const items = {};
  let data1 = data.data.dataList;
  data1.map((item, index) => {
    const id = GSTCID(item.id);
    let row = {};
    row.id = GSTCID(item.id);
    row.task = item.name;
    row.startTime = date(item.datastart).valueOf();
    row.endTime = date(item.dataend).valueOf();
    row.taskTime = item.allday?.toString();
    row.parentId =
      item.parent && item.parent != item.id ? GSTCID(item.parent) : null;
    row.level = item.code.length / 2;
    row.code = item.code;
    row.expanded = false;
    row.prepath = item.prepath;
    rows[id] = row;
    let item_1 = {};
    item_1.id = GSTCID(item.id);
    item_1.rowId = GSTCID(item.id);
    item_1.label = item.id;
    item_1.time = {};
    item_1.time.start = date(item.datastart).add(8, "hour").valueOf();
    item_1.time.end = setEndTime(
      date(item.datastart).add(8, "hour").valueOf()
    ).valueOf();
    item_1.style =
      row.level != 5
        ? { display: "none", backgroundColor: "#7E349D" }
        : { backgroundColor: "#7E349D" };
    item_1.dependant = [];
    item_1.labelFunc = labelFunc;
    item_1.lineContent = `${row.task}-(${row.taskTime})`;
    items[id] = item_1;
  });
  for (let i in items) {
    line.map((li, index) => {
      if (li.source == i.split("-")[1] && rows[i].level === 5) {
        items[i].dependant.push(GSTCID(li.target));
      }
    });
  }
  return { rows, items };
};
const getLevelData4 = async () => {
  const line = await getLins();
  console.log(line);
  const { data } = await axios.get(`/api/dataimport/listparent`);
  const rows = {};
  const items = {};
  let data1 = data.data.dataList;
  data1.map((item, index) => {
    const id = GSTCID(item.id);
    let row = {};
    row.id = GSTCID(item.id);
    row.task = item.name;
    row.startTime = date(item.datastart).valueOf();
    row.endTime = date(item.dataend).valueOf();
    row.taskTime = item.allday?.toString();
    row.parentId =
      item.parent && item.parent != item.id ? GSTCID(item.parent) : null;
    row.level = item.code.length / 2;
    row.code = item.code;
    row.expanded = false;
    row.prepath = item.prepath;
    rows[id] = row;
    let item_1 = {};
    item_1.id = GSTCID(item.id);
    item_1.rowId = GSTCID(item.id);
    item_1.label = item.id;
    item_1.time = {};
    item_1.time.start = date(item.datastart).add(8, "hour").valueOf();
    item_1.time.end = setEndTime(
      date(item.datastart).add(8, "hour").valueOf()
    ).valueOf();
    item_1.style =
      row.level != 4
        ? { display: "none", backgroundColor: "#7E349D" }
        : { backgroundColor: "#7E349D" };
    item_1.dependant = [];
    item_1.labelFunc = labelFunc;
    item_1.lineContent = `${row.task}-(${row.taskTime})`;
    items[id] = item_1;
  });
  for (let i in items) {
    line.map((li, index) => {
      if (li.source == i.split("-")[1] && rows[i].level === 4) {
        items[i].dependant.push(GSTCID(li.target));
      }
    });
  }
  return { rows, items };
};
const getLevelData1 = async () => {
  const line = await getLins();
  console.log(line);
  const { data } = await axios.get(`/api/dataimport/listparent`);
  const rows = {};
  const items = {};
  let data1 = data.data.dataList;
  data1.map((item, index) => {
    const id = GSTCID(item.id);
    let row = {};
    row.id = GSTCID(item.id);
    row.task = item.name;
    row.startTime = date(item.datastart).valueOf();
    row.endTime = date(item.dataend).valueOf();
    row.taskTime = item.allday?.toString();
    row.parentId =
      item.parent && item.parent != item.id ? GSTCID(item.parent) : null;
    row.level = item.code.length / 2;
    row.code = item.code;
    row.expanded = false;
    row.prepath = item.prepath;
    rows[id] = row;
    let item_1 = {};
    item_1.id = GSTCID(item.id);
    item_1.rowId = GSTCID(item.id);
    item_1.label = item.id;
    item_1.time = {};
    item_1.time.start = date(item.datastart).add(8, "hour").valueOf();
    item_1.time.end = setEndTime(
      date(item.datastart).add(8, "hour").valueOf()
    ).valueOf();
    item_1.style =
      row.level != 1
        ? { display: "none", backgroundColor: "#7E349D" }
        : { backgroundColor: "#7E349D" };
    item_1.dependant = [];
    item_1.labelFunc = labelFunc;
    item_1.lineContent = `${row.task}-(${row.taskTime})`;
    items[id] = item_1;
  });
  for (let i in items) {
    line.map((li, index) => {
      if (li.source == i.split("-")[1] && rows[i].level === 1) {
        items[i].dependant.push(GSTCID(li.target));
      }
    });
  }
  return { rows, items };
};
const getLevelData2 = async () => {
  const line = await getLins();
  console.log(line);
  const { data } = await axios.get(`/api/dataimport/listparent`);
  const rows = {};
  const items = {};
  let data1 = data.data.dataList;
  data1.map((item, index) => {
    const id = GSTCID(item.id);
    let row = {};
    row.id = GSTCID(item.id);
    row.task = item.name;
    row.startTime = date(item.datastart).valueOf();
    row.endTime = date(item.dataend).valueOf();
    row.taskTime = item.allday?.toString();
    row.parentId =
      item.parent && item.parent != item.id ? GSTCID(item.parent) : null;
    row.level = item.code.length / 2;
    row.code = item.code;
    row.expanded = item.code.length / 2 === 1 ? true : false;
    row.prepath = item.prepath;
    rows[id] = row;
    let item_1 = {};
    item_1.id = GSTCID(item.id);
    item_1.rowId = GSTCID(item.id);
    item_1.label = item.id;
    item_1.time = {};
    item_1.time.start = date(item.datastart).add(8, "hour").valueOf();
    item_1.time.end = setEndTime(
      date(item.datastart).add(8, "hour").valueOf()
    ).valueOf();
    item_1.style =
      row.level != 2
        ? { display: "none", backgroundColor: "#7E349D" }
        : { backgroundColor: "#7E349D" };
    item_1.dependant = [];
    item_1.labelFunc = labelFunc;
    item_1.lineContent = `${row.task}-(${row.taskTime})`;
    items[id] = item_1;
  });
  for (let i in items) {
    line.map((li, index) => {
      if (li.source == i.split("-")[1] && rows[i].level === 2) {
        items[i].dependant.push(GSTCID(li.target));
      }
    });
  }
  return { rows, items };
};
const getRootData = async () => {
  const line = await getLins();
  console.log(line);
  const { data } = await axios.get(`/api/dataimport/listparent`);
  const rows = {};
  const items = {};
  let data1 = data.data.dataList;
  data1.map((item, index) => {
    const id = GSTCID(item.id);
    let row = {};
    row.id = GSTCID(item.id);
    row.task = item.name;
    row.startTime = date(item.datastart).valueOf();
    row.endTime = date(item.dataend).valueOf();
    row.taskTime = item.allday?.toString();
    row.parentId =
      item.parent && item.parent != item.id ? GSTCID(item.parent) : null;
    row.level = item.code.length / 2;
    row.code = item.code;
    row.expanded =
      (item.code.length / 2 === 1 && item.id === 1) || item.id === 2
        ? true
        : false;
    row.prepath = item.prepath;
    rows[id] = row;
    let item_1 = {};
    item_1.id = GSTCID(item.id);
    item_1.rowId = GSTCID(item.id);
    item_1.label = item.id;
    item_1.time = {};
    item_1.time.start = date(item.datastart).add(8, "hour").valueOf();
    item_1.time.end = setEndTime(
      date(item.datastart).add(8, "hour").valueOf()
    ).valueOf();
    item_1.style =
      row.level != 3
        ? { display: "none", backgroundColor: "#7E349D" }
        : { backgroundColor: "#7E349D" };
    item_1.dependant = [];
    item_1.labelFunc = labelFunc;
    item_1.lineContent = `${row.task}-(${row.taskTime})`;
    items[id] = item_1;
  });
  for (let i in items) {
    line.map((li, index) => {
      if (li.source == i.split("-")[1] && rows[i].level === 3) {
        items[i].dependant.push(GSTCID(li.target));
      }
    });
  }
  return { rows, items };
};
const addmethod = async (data) => {
  const res = await axios.post(`/api/dataimport/add`, data);
};
const deleteData = async (data) => {
  const res = await axios.delete(`/api/dataimport/delete`, {
    params: data,
  });
};
const updateMethod = async (data) => {
  const res = await axios.put(`/api/dataimport/updata`, data);
};
const changePath = async (data) => {
  const res = await axios.put(`/api/demolink/add`, data);
  console.log("path", res);
};
export {
  GSTCID,
  getRootData,
  generate,
  addmethod,
  deleteData,
  updateMethod,
  getLins,
  changePath,
  getLevelData2,
  getLevelData1,
  getLevelData4,
  getLevelData5,
  getLevelData6,
};
